package com.digital.herbarium.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.digital.herbarium.Entity.Image;
import com.digital.herbarium.Entity.Stat;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.Mapper.ImageMapper;
import com.digital.herbarium.Util.ExcelUtilHandler;
import com.digital.herbarium.Util.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ImageService {

    @Autowired
    private ImageMapper imageMapper;


    public PageInfo<Image> getImageInfo (Image image, int pageSize, int pageNum){
        PageHelper.startPage(pageNum, pageSize);
        List<Image> ilist =  imageMapper.getImageInfo(image);
        PageInfo<Image> pageInfo = new PageInfo<Image>(ilist);
        return  pageInfo;
    }

    public void addImageInfo(Image image){
        image.setState(1);
        image.setCreatetime(TimeUtil.getNow());
        imageMapper.addImageInfo(image);
    }

    public void editImageInfo(Image image){
         imageMapper.editImageInfo(image);
    }

    public void delImageInfo(String id){
        if (StrUtil.hasEmpty(id) || StrUtil.hasBlank(id)) {
            throw new CustomException(400, "缺少信息id");
        }
        String[] strArr = id.split(",");
        Long[] ids = Convert.toLongArray(strArr);
        imageMapper.delImageInfo(ids);
    }

    public void  addExcel(MultipartFile file, HttpServletRequest request, String uploadPath){
        ExcelReader reader = ExcelUtilHandler.getExcelReader(file, request, uploadPath);
        reader.addHeaderAlias("物种中文名","cnName");
        reader.addHeaderAlias("物种拉丁名","ldName");
        reader.addHeaderAlias("属中文名","genCnName");
        reader.addHeaderAlias("属拉丁名","genLdName");
        reader.addHeaderAlias("科中文名","specCnName");
        reader.addHeaderAlias("科拉丁名","specLdName");
        reader.addHeaderAlias("图片编号","imageCode");
        reader.addHeaderAlias("图片分类","imageType");
        List<Image> imageList = reader.readAll( Image.class);
        imageList.stream().forEach(image -> {
            image.setCreatetime(TimeUtil.getNow());
        });
        imageMapper.bachAdd(imageList);


    }

    public Integer getTotal(){
        return imageMapper.getTotal(null);
    }

    public Integer getYesterdayTotal(){
        String yesterday = TimeUtil.getYesterday();
        return imageMapper.getTotal(yesterday);
    }

    public Integer getNowTotal(){
        String now = TimeUtil.getNow();
        return imageMapper.getTotal(now);
    }

    public  List<Stat> getStatGroupByType(){
        List<Stat> slist = imageMapper.getStatGroupByType();
        Integer total = imageMapper.getTotal(null);
        slist.stream().forEach(stat -> {
            stat.setValue(stat.getValue() / total * 100);
        });
        return  slist;
    }

    public List<Stat> getTotalGroupByDate(String startTime,String endTime){
        return imageMapper.getTotalGroupByDate(startTime,endTime);
    }


}
